package com.github.cyf.controller;

import com.github.cyf.common.model.Rs;
import com.github.cyf.common.validator.EnumString;
import com.github.cyf.entity.User;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;

/**
 * https://developer.aliyun.com/article/786719
 * @author chenyifan
 * @create 2024-05-02 17:33
 */
@RestController
@RequestMapping("/validated")
@Validated
@Api(tags = "1、校验参数")
@ApiSupport(order = 1)
public class ValidatedController {

    @PostMapping("/user")
    @ApiOperation("请求体校验")
    public Rs validatedUser(@RequestBody @Validated User user) {
        /*
            {
              "age": 10,
              "appId": "134253",
              "email": "320@qq.com",
              "level": "12",
              "money": 0,
              "name": "cyf",
              "phone": "15191377955",
              "weekDay": 2
            }
        */
        return Rs.ok("校验成功").setData(user);
    }

    @PostMapping("/name")
    @ApiOperation("请求参数校验")
    public Rs validatedName(@RequestParam @NotBlank(message = "参数username不能为空") String username) {
        return Rs.ok("校验成功").setData(username);
    }

    @PostMapping("/enum")
    @ApiOperation("自定义注解校验")
    public Rs validatedEnum(@RequestParam @EnumString(value = {"A", "B", "C"}, message = "e1只允许为A,B,C") String e1,
                            @RequestParam @EnumString(value = {"F", "M"}, message = "e2只允许为F,M") String e2) {
        return Rs.ok("校验成功").set("e1", e1).set("e2", e2);
    }
}
